package weibo.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import weibo.common.result.JsonResult;
import weibo.common.result.StatusCode;
import weibo.mapper.UserMapper;
import weibo.pojo.dto.UserLogin;
import weibo.pojo.dto.UserRegDTO;
import weibo.pojo.entity.User;
import weibo.pojo.vo.UserVO;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Api(tags = "01.用户模块")
@RestController
@RequestMapping("/v1/users")
@Slf4j
public class UserController {

    @Autowired
    private UserMapper userMapper;

    // 注册功能  状态码：1-成功，2-账号已存在
    @PostMapping ("/reg")
    @ApiOperation(value = "注册功能")
    public JsonResult reg(@RequestBody @Validated UserRegDTO userRegDTO){
        log.info("用户注册，用户信息：{}",userRegDTO);
        // 判断账号是否已存在
        User userReg=userMapper.selectByUsername(userRegDTO.getUsername());
        log.debug("查询到的用户信息：{}",userReg);
        if(userReg!=null){
            // 账号已存在
            log.info("注册时：账号已存在");
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }

        User user=new User();
        BeanUtils.copyProperties(userRegDTO,user);
        user.setCreated(new Date());
        log.debug("要插入的数据：{}",user);
        userMapper.insert(user);
        log.info("用户注册成功");
        return JsonResult.success();
    }

    // 登录功能  状态码：1-成功，2-账号不存在，3-密码错误
    @PostMapping("/login")
    @ApiOperation(value = "登录功能")
    public JsonResult login(@RequestBody @Validated UserLogin userLogin, @ApiIgnore HttpSession session){
        // 查询用户
        log.info("用户登录，用户信息：{}",userLogin);
        User user=userMapper.selectByUsername(userLogin.getUsername());
        log.debug("查询到的用户信息：{}",user);
        if (user==null){
            // 账号不存在
            return new JsonResult(StatusCode.USERNAME_ERROR);
        }else if (!user.getPassword().equals(userLogin.getPassword())){
            // 密码错误
            return new JsonResult(StatusCode.PASSWORD_ERROR);
        }
        log.info("用户登录成功,保存用户的登录信息");
        session.setAttribute("user",user);
        return JsonResult.success();
    }

    // 获取登录状态
    @GetMapping("/currentUser")
    @ApiOperation(value = "获取登录状态")
    public JsonResult currentUser(@ApiIgnore HttpSession session){
        User user=(User) session.getAttribute("user");
        log.info("获取登录状态，用户信息：{}",user);
        if (user==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        UserVO userVO=new UserVO();
        BeanUtils.copyProperties(user,userVO);
        log.debug("返回的用户信息：{}",userVO);
        log.info("获取登录状态成功");
        return JsonResult.success(userVO);
    }

    // 退出登录
    @GetMapping("/logout")
    @ApiOperation(value = "退出登录")
    public void logout(@ApiIgnore HttpSession session){
        log.info("用户退出登录");
        session.removeAttribute("user");
    }


}
